package com.ruitu.mall_98_2.mvp.presenter;

import com.beanu.arad.utils.Logg;
import com.beanu.arad.utils.ToastUtils;
import com.ruitu.mall_98_2.mvp.contract.CommentContract;
import com.ruitu.router_module.bean.api.BaseResult;
import com.ruitu.router_module.bean.api.SubscriberImpl;

import java.io.File;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class CommentPresenter extends CommentContract.Presenter {
    @Override
    public void saveCommentScore(MultipartBody multipartBody) {
        mModel.saveCommentScore(multipartBody)
                .subscribe(new SubscriberImpl<BaseResult>() {
                    @Override
                    public void onError(Throwable e) {
                        mView.hideProgress();
                        Logg.i(e.toString());
                        ToastUtils.showShort("评论失败");
                    }

                    @Override
                    public void onNext(BaseResult baseResult) {
                        mView.hideProgress();
                        if (baseResult.isSuccess()) {
                            mView.setCommentSuc();
                        } else {
                            ToastUtils.showShort(baseResult.getMsg());
                        }
                    }
                });
    }

    @Override
    public MultipartBody saveCommentScore(String odId, String accountId, String goodsComment, String companyComment, String logisticsComment, String content, String goodsId, String goodsModelId, List<File> commentPictures) {
        MediaType FORM_CONTENT_TYPE = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8");
        MultipartBody.Builder builder = new MultipartBody.Builder();
        builder.addPart(MultipartBody.Part.createFormData("odId", null, RequestBody.create(FORM_CONTENT_TYPE, odId)));
        builder.addPart(MultipartBody.Part.createFormData("accountId", null, RequestBody.create(FORM_CONTENT_TYPE, accountId)));
        builder.addPart(MultipartBody.Part.createFormData("goodsComment", null, RequestBody.create(FORM_CONTENT_TYPE, goodsComment)));
        builder.addPart(MultipartBody.Part.createFormData("companyComment", null, RequestBody.create(FORM_CONTENT_TYPE, companyComment)));
        builder.addPart(MultipartBody.Part.createFormData("logisticsComment", null, RequestBody.create(FORM_CONTENT_TYPE, logisticsComment)));
        if (content != null) {
            builder.addPart(MultipartBody.Part.createFormData("content", null, RequestBody.create(FORM_CONTENT_TYPE, content)));
        }
        builder.addPart(MultipartBody.Part.createFormData("goodsId", null, RequestBody.create(FORM_CONTENT_TYPE, goodsId)));
        if (goodsModelId != null) {
            builder.addPart(MultipartBody.Part.createFormData("goodsModelId", null, RequestBody.create(FORM_CONTENT_TYPE, goodsModelId)));
        }
        if (commentPictures != null) {
            for (int i = 0; i < commentPictures.size(); i++) {
                RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), commentPictures.get(i));
                builder.addFormDataPart("commentPictures", commentPictures.get(i).getName(), requestBody);
            }
        }
        MultipartBody multipartBody = builder.build();

        return multipartBody;
    }
}
